<template>
  <!-- 微信公众号文字列表 -->
  <el-container>
    <el-header>
      <div class="left-panel">
        <el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
      </div>
      <!-- <div class="right-panel">
        <div class="right-panel-search">
          <el-input
            v-model="searchValue.search"
            placeholder="类别名称"
            clearable
          ></el-input>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="upsearch"
          ></el-button>
        </div>
      </div> -->
    </el-header>
    <el-main class="nopadding">
      <scTable
        ref="table"
        row-key="id"
        :apiObj="apiObj"
        @row-dblclick="handleRowDblclick"
      >
        <el-table-column label="ID" prop="id" width="80"></el-table-column>
        <el-table-column
          label="标题"
          prop="title"
          min-width="100"
        ></el-table-column>
        <el-table-column
          label="作者"
          prop="author"
          width="200"
        ></el-table-column>
        <el-table-column
          label="摘要"
          prop="digest"
          min-width="100"
        ></el-table-column>
        <el-table-column
          label="创建日期"
          prop="createTime"
          width="250"
        ></el-table-column>
        <el-table-column label="发布状态" prop="status" width="200">
          <!-- 	发布状态：0未发布，1发布成功，2发布未通过 -->
          <template #default="scope">
            <p v-if="scope.row.status == 0">未发布</p>
            <p v-if="scope.row.status == 1" style="color: #67c23a">发布成功</p>
            <p v-if="scope.row.status == 2" style="color: #f56c6c">
              发布未通过
            </p>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="right" width="250">
          <template #default="scope">
            <el-button-group>
              <el-button
                text
                type="primary"
                size="small"
                @click="table_publish(scope.row, scope.$index)"
                >发布</el-button
              >
              <el-button
                text
                type="primary"
                size="small"
                @click="table_status(scope.row, scope.$index)"
                v-if="scope.row.type != 0"
                >获取状态</el-button
              >
              <el-button
                text
                type="primary"
                size="small"
                @click="table_show(scope.row, scope.$index)"
                >查看</el-button
              >
              <el-button
                text
                type="primary"
                size="small"
                @click="table_edit(scope.row, scope.$index)"
                >编辑</el-button
              >
              <el-popconfirm
                title="确定删除吗？"
                @confirm="table_del(scope.row, scope.$index)"
              >
                <template #reference>
                  <el-button text type="primary" size="small">删除</el-button>
                </template>
              </el-popconfirm>
            </el-button-group>
          </template>
        </el-table-column>
      </scTable>
    </el-main>
  </el-container>
</template>

<script>

export default {
  name: 'artcle',
  data () {
    return {
      apiObj: this.$API.weChat.getArtcleList,
      searchValue: {
        search: null
      },

    }
  },
  methods: {
    //添加
    add () {
      this.$router.push({
        path: '/weChat/article/add',
        query: {
          mode: 'add'
        }
      })
    },
    // 发布文章
    async table_publish (row) {
      let res = await this.$API.weChat.publishArtcle.post({ id: row.id })
      console.log(res)
      if (res.code == 200) {
        this.$refs.table.refresh()
      }

    },
    // 获取状态
    async table_status (row) {
      console.log(row)
      // let res = await this.$API.weChat.getArticleStatus.get({ id: row.id, publishId: '' })
      // console.log(res)
      // if (res.code == 200) {
      //   this.$refs.table.refresh()
      // }
    },
    //编辑
    table_edit (row) {
      this.$router.push({
        path: '/weChat/article/add',
        query: {
          id: row.id,
          mode: 'edit'
        }
      })
    },
    //查看
    table_show (row) {
      this.$router.push({
        path: '/weChat/article/detail',
        query: {
          id: row.id,
        }
      })
    },
    //删除
    async table_del (row) {
      var reqData = { id: row.id }
      var res = await this.$API.weChat.deleteArtcle.post(reqData)
      if (res.code == 200) {
        this.$refs.table.refresh()
        this.$message.success("删除成功")
      }
    },

    //搜索
    upsearch () {
      this.$refs.table.upData(this.searchValue)
    },


    //表格内开关事件
    changeSwitch (row) {
      console.log(row)
      return new Promise((resolve, reject) => {
        let data = {
          id: row.id,
          state: row.state == 0 ? 1 : 0
        }
        this.$API.goods.updateUnit.post(data).then(res => {
          if (res.code == 200) {
            this.$message.success("操作成功")
            this.$refs.table.refresh()
            resolve(true)
          } else {
            this.$refs.table.refresh()
            reject(false)
          }
        })

      })
    },

    //本地更新数据
    handleSaveSuccess (data, mode) {
      if (mode == 'add') {
        this.$refs.table.refresh()
      } else if (mode == 'edit') {
        this.$refs.table.refresh()
      }
    },

    handleRowDblclick (row) {
      this.table_show(row)
    }


  }
}
</script>

<style>
</style>
